import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';


/*
 * @Author: ZhongWb
 * @Description: 底部弹出日期、时间选择器使用封装
 * @Datetime: 2019/12/27 10:13
 * @Version: 1.0
 **/
DateTimeButtom (context,{
    String dateFormat,//格式化
    DateTime maxDateTime,//最大日期
    DateTime minDateTime,//最小日期
    DateTimePickerMode pickerMode,//显示模式
    Function selectDateStr,//选择的
    String defDateTime,//默认日期 为null时 默认显示今天
  }) async {
  var dateStr = "";
  DatePicker.showDatePicker(context,
    //配置中文
    locale: DateTimePickerLocale.zh_cn,
    //模式切换
    pickerMode:pickerMode ?? DateTimePickerMode.date,
    pickerTheme: DateTimePickerTheme(
      cancel: Text("取消"),
      confirm: Text("确定"),
    ),
    //初始日期时间
    initialDateTime: defDateTime!=null && defDateTime!=""?DateTime.parse(defDateTime):DateTime.now(),
    //最小日期时间
    minDateTime: minDateTime ?? DateTime.parse("1800-01-01"),
    //最大日期时间
    maxDateTime: maxDateTime ?? DateTime.now(),
    //显示的样式 更多样式 参考 https://blog.csdn.net/weixin_38025168/article/details/103726649
    dateFormat: dateFormat ?? 'yyyy MM dd',
    onCancel:(){},
    onClose:(){},
    onChange:(data,i){print(data);},
    onConfirm:(data,i){
      String month = "";
      String day = "";
      //月小于10 补0
      if (data.month<10)
        month = "0"+data.month.toString();
      else
        month = data.month.toString();
      //日小于10 补0
      if (data.day<10)
        day = "0"+data.day.toString();
      else
        day = data.day.toString();
      dateStr = data.year.toString()+"-"+month+"-"+day;
      print("选择的时间参数："+dateStr);
      selectDateStr(dateStr);

    },
  );
}
